package com.project.springboot.controller;

import com.gaoice.easyexcel.spring.boot.autoconfigure.ExcelFile;
import com.gaoice.easyexcel.spring.boot.autoconfigure.annotation.ResponseExcel;
import com.gaoice.easyexcel.writer.SheetInfo;
import com.project.springboot.entity.MyStudent;
import com.project.springboot.entity.MyTeacher;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * todo 注意：在Controller上使用@RestController或者@ResponseBody会导致starter失效
 * description: 主要是测试easyexcel导出Excel
 * @author 朱正磊
 * @date 2023-09-01 12:51:19
 */
@Controller
@RequestMapping("/myStudent")
public class MyStudentController {

    /**
     * description: 在内存中构造测试数据
     * @author 朱正磊
     * @date 2023-09-01 14:35:58
     * @return 返回测试数据
     */
    private List<MyStudent> listMyStudent() {
        ArrayList<MyStudent> list = new ArrayList<>();

        MyTeacher myTeacher = new MyTeacher().setName("唐三藏");

        MyStudent myStudent1 = new MyStudent().setName("孙悟空").setNo("001").setAge(500).setMyTeacher(myTeacher);
        MyStudent myStudent2 = new MyStudent().setName("猪八戒").setNo("002").setAge(300).setMyTeacher(myTeacher);
        MyStudent myStudent3 = new MyStudent().setName("沙悟净").setNo("003").setAge(200).setMyTeacher(myTeacher);
        list.add(myStudent1);
        list.add(myStudent2);
        list.add(myStudent3);

        for (int i = 4; i <= 100; i++) {
            MyStudent myStudent = new MyStudent().setName("刘德华" + i).setNo("00" + i).setAge(10 + i).setMyTeacher(myTeacher);
            list.add(myStudent);
        }

        return list;
    }

    /**
     * description: 使用注解@ResponseExcel进行导出Excel
     * 注解@ResponseExcel的必须值：fieldNames
     *      sheetName默认值为default
     *      fileName默认使用sheetName的值
     *      columnNames默认使用fieldNames的值
     *      sheetStyle可以指定样式，默认为DefaultSheetStyle.class
     * 生效条件：
     *      配置文件enable-response-excel=true
     *      使用@ResponseExcel注解
     *      不使用@ResponseBody注解
     * @author 朱正磊
     * @date 2023-09-01 14:23:51
     * @return 返回需要导出的数据
     */
    @GetMapping("/exportByResponseExcel")
    @ResponseExcel(fileName = "使用ResponseExcel导出Excel",
            sheetName = "学生信息",
            columnNames = {"学生姓名", "学号", "年龄", "老师姓名"},
            fieldNames = {"name", "no", "age", "myTeacher.name"})
    public List<MyStudent> exportByResponseExcel() {
        return this.listMyStudent();
    }

    /**
     * description: 使用SheetInfo进行导出Excel
     * 文件名默认使用sheetName
     * 生效条件：
     *      enable-sheet-info=true
     *      返回值类型为SheetInfo
     *      不使用@ResponseBody注解
     * @author 朱正磊
     * @date 2023-09-01 15:15:56
     * @return 返回SheetInfo对象
     */
    @GetMapping("/exportBySheetInfo")
    public SheetInfo exportBySheetInfo() {
        String sheetName = "使用SheetInfo进行导出Excel";
        String[] columnNames = {"学生姓名", "学号", "年龄"};
        String[] fieldNames = {"name", "no", "age"};
        List<MyStudent> myStudentList = this.listMyStudent();
        return new SheetInfo(sheetName, columnNames, fieldNames, myStudentList);
    }

    /**
     * description: 使用ExcelFile进行导出Excel
     * 生效条件：
     *      enable-excel-file=true
     *      返回值类型为ExcelFile
     *      不使用@ResponseBody注解
     * @author 朱正磊
     * @date 2023-09-01 15:15:56
     * @return 返回ExcelFile对象
     */
    @GetMapping("/exportByExcelFile")
    public ExcelFile exportByExcelFile() {
        List<MyStudent> myStudentList = this.listMyStudent();

        String sheetName1 = "sheet1";
        String[] columnNames1 = {"学生姓名", "学号", "年龄"};
        String[] fieldNames1 = {"name", "no", "age"};
        SheetInfo sheetInfo1 = new SheetInfo(sheetName1, columnNames1, fieldNames1, myStudentList);

        String sheetName2 = "sheet2";
        String[] columnNames2 = {"学生姓名", "学号", "年龄", "老师姓名"};
        String[] fieldNames2 = {"name", "no", "age", "myTeacher.name"};
        SheetInfo sheetInfo2 = new SheetInfo(sheetName2, columnNames2, fieldNames2, myStudentList);

        ExcelFile excelFile = new ExcelFile();
        excelFile.setFileName("使用ExcelFile进行导出Excel");
        excelFile.addSheet(sheetInfo1);
        excelFile.addSheet(sheetInfo2);

        return excelFile;
    }

}
